package com.stone.design.patterns.state;

/**
 * @author Stone
 * @version V1.0
 * @description:
 * @date 4/25/18 22:13
 * @email redsstone@163.com
 */
public class GumballMachine {

    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;

    State mState = soldOutState; // 初始状态
    int count = 0;
    String location;


    public GumballMachine(String location, int count) {

        this.location = location;
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldOutState = new SoldOutState(this);
        this.count = count;
        if (count > 0) {
            // 有糖果时候的状态
            mState = noQuarterState;
        }
    }

    public State getState() {
        return mState;
    }

    public String getLocation() {
        return location;
    }

    public void insertQuarter() {
        mState.insertQuarter();

    }

    public void ejectQuarter() {
        mState.ejectQuarter();
    }

    public void tumCrank() {
        mState.tumCrank();
        mState.dispense();
    }


    public void setState(State state) {
        mState = state;
    }

    void releaseBall() {
        if (mState == soldState) {
            System.out.println("正在售卖");
            if (count != 0) {
                count = count - 1;
            }
        }

    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public int getCount() {
        return count;
    }
}
